﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace atmsystem_ss
{
    public partial class frmWithdraw : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dtAccount = new DataTable("tblAccount");
        private DataTable dtCustomer = new DataTable("tblCustomer");
        private DataTable dtTransaction = new DataTable("tblTransaction");
        private DataTable dtTransaction_Check = new DataTable("tblTransaction");
        private SqlDataAdapter da = new SqlDataAdapter();
        private int Sum_Currency_Transaction;
        private int Lan_Currency_Transaction;
        public string strID_Account { set; get; }
        private Boolean kt;
        private Boolean test;


        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        private void hienthiFull_Name()
        {
            dtCustomer.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select distinct fldFull_Name from tblCustomer join tblAccount on tblCustomer.fldID_Customer = tblAccount.fldID_Customer where fldID_Account LIKE '%'+@fldID_Account+'%' ";
            command.Parameters.Add("@fldID_Account", SqlDbType.VarChar, 50).Value = strID_Account;
            da.SelectCommand = command;
            da.Fill(dtCustomer);
            label1.DataBindings.Add("Text", dtCustomer, "fldFull_Name", true);
        }
        public frmWithdraw()
        {
            InitializeComponent();
        }
        private bool IsNumber(String str)
        {
            foreach (Char c in str)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
        private bool kiemtra()
        {
            check_lanrut();
            kt = true;
            if (txtCurrency_Sub.Text == "")
            {
                txtCurrency_Sub.Focus();
                MessageBox.Show("Ban hay nhap so tien ban can rut","Thong bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (IsNumber(txtCurrency_Sub.Text) == false)
            {
                txtCurrency_Sub.Text = "";
                txtCurrency_Sub.Focus();
                MessageBox.Show("Ban hay nhap so tien ban can rut", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (test == false)
            {
                if (MessageBox.Show("ban khong the rut tien duoc nua, do ban da rut qua 5 lan/ngay hoac da rut qua 25000, Click Ok de tro lai trang giao dich", "Thong bao", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    frmCustomer_Account _frmCustomer_Account = new frmCustomer_Account();
                    _frmCustomer_Account.Show();
                    Hide();              
                }
                kt = false;
            }
            return kt;

        }
        private bool check_lanrut() {
            test = true;
            textBox1.Clear();
            textBox2.Clear();
            dtTransaction_Check.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select sum(fldCurrency_Transaction) as N'Sum_Currency_Transaction', count(fldID_Transaction)as N'Lan_Transaction' from tblTransaction where (fldID_Account=@fldID_Account) and (fldType_Transaction='Withdraw') and ((convert(varchar(50),fldDate, 103)) = (convert(varchar(50),getdate(), 103))) group by (convert(varchar(50),fldDate, 103)) ";
            command.Parameters.Add("@fldID_Account", SqlDbType.VarChar, 50).Value = strID_Account;
            da.SelectCommand = command;
            da.Fill(dtTransaction_Check);
            if (dtTransaction_Check.Rows.Count >0)
            {

                textBox1.DataBindings.Add("Text", dtTransaction_Check, "Sum_Currency_Transaction", true);
                textBox2.DataBindings.Add("Text", dtTransaction_Check, "Lan_Transaction", true);

                Sum_Currency_Transaction = Convert.ToInt32(textBox1.Text);
                Lan_Currency_Transaction = Convert.ToInt32(textBox2.Text);
                if (Sum_Currency_Transaction > 25000)
                {
                    test= false;
                }
                else if (Lan_Currency_Transaction > 5)
                {
                    test = false;
                }
              
            }
            return test;            

        }
        private void frmWithdraw_Load(object sender, EventArgs e)
        {
            connect();
            hienthiFull_Name();
        }

        private void btncomplete_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "InsertTransaction_UpdateAccount_Withdraw";
                cmd.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = strID_Account.ToString();
                cmd.Parameters.Add("@fldCurrency_Transaction", SqlDbType.Int, 50).Value = Convert.ToInt32(txtCurrency_Sub.Text);
                SqlDataReader daReader = cmd.ExecuteReader();
                dtTransaction.Load(daReader);
                //MessageBox.Show("Complete", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frmTransaction _frmTransaction = new frmTransaction();
                _frmTransaction.Show();
                Hide();
                txtCurrency_Sub.Text = "";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmCustomer_Account _frmCustomer_Account = new frmCustomer_Account();
            _frmCustomer_Account.Show();
            Hide();
        }
    }
}
